﻿using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace Storm.TextEditor.Parser.XML
{
    /// <summary>
    /// Represents a list of Language.
    /// </summary>
    public class LanguageList
        : IEnumerable
    {
        #region Fields

        private Dictionary<string, Language> languages = new Dictionary<string, Language>();

        #endregion

        #region Methods

        #region Public

        /// <summary>
        /// Returns the Language at the given index.
        /// </summary>
        /// <param name="index">Index to find a Language at.</param>
        /// <returns>The Language at the given index.</returns>
        public Language GetFromIndex(int index)
        {
            if (index >= 0 && index < languages.Count)
            {
                string[] keys = new string[languages.Keys.Count];
                languages.Keys.CopyTo(keys, 0);

                return languages[keys[index]];
            }

            return null;
        }

        /// <summary>
        /// Returns the Language with the given name.
        /// </summary>
        /// <param name="name">Name of the Language.</param>
        /// <returns>The Language with the given name.</returns>
        public Language GetLanguageFromName(string name)
        {
            if (languages.ContainsKey(name) == false)
                return null;

            return languages[name];
        }

        /// <summary>
        /// Returns the Language in the given file.
        /// </summary>
        /// <param name="path">File to find a Language in.</param>
        /// <returns>The Language in the given file.</returns>
        public Language GetLanguageFromFile(string path)
        {
            string extension = Path.GetExtension(path);
            foreach (Language lang in languages.Values)
            {
                foreach (FileType fileType in lang.FileTypes)
                {
                    if (extension.ToLower() == fileType.Extension.ToLower())
                        return lang;
                }
            }

            return null;
        }

        /// <summary>
        /// Adds the given Language to the LanguageList.
        /// </summary>
        /// <param name="lang">Language to add.</param>
        public void Add(Language lang)
        {
            if (languages.ContainsKey(lang.Name) == false)
                languages.Add(lang.Name, lang);
        }

        /// <summary>
        /// Returns the IEnumerator that represents the LanguageList's local Enumerator.
        /// </summary>
        /// <returns>The IEnumerator that represents the LanguageList's local Enumerator.</returns>
        public IEnumerator ListLanguages()
        {
            return languages.GetEnumerator();
        }

        #endregion

        #endregion

        /// <summary>
        /// Initializes a new instance of LanguageList.
        /// </summary>
        public LanguageList()
        {
        }

        /// <summary>
        /// Initializes a new instance of LanguageList.
        /// </summary>
        /// <param name="directory">Directory to add languages from.</param>
        /// <param name="ext">Extension of the file the loader should search for.</param>
        public LanguageList(string directory, string ext)
        {
            string[] files = Directory.GetFiles(directory, ext);
            foreach (string file in files)
            {
                LanguageReader loader = new LanguageReader();
                Language language = loader.Load(file);

                if (languages.ContainsKey(language.Name) == false)
                    languages.Add(language.Name, language);
            }
        }

        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        { return languages.GetEnumerator(); }

        #endregion
    }
}
